home *** CD-ROM | disk | FTP | other *** search
- Path: svnews.ubinet.ubs.com!ubszh!ubszh!jis
- From: ian.johnston@ubs.com (Johnston Ian (by ubsswop))
- Newsgroups: comp.lang.c++
- Subject: Re: How to handle error in constructor
- Date: 31 Jan 1996 10:42:39 GMT
- Organization: UBS
- Distribution: world
- Message-ID: <4enh2v$plq@ubszh.fh.zh.ubs.com>
- References: <DLyyIM.5EG@teslab.lab.oz.au>
- NNTP-Posting-Host: nol2179.fh.zh.ubs.com
-
- In article <DLyyIM.5EG@teslab.lab.oz.au>, andrew@teslab.lab.oz.au (Andrew Phillips) writes:
- |> I'm converting and enhancing a simple program in C to C++. Without going
- |> into too much detail I have a class that represents a file on disk. The
- |> contructor opens the file, but what should it do if the file cannot be
- |> opened? The C program just calls fopen() tests the return value and
- |> if there's an error it prints a message and exits.
- |>
- |> In C++ I thought to set a flag in the constructor and have a member function
- |> that tests the flag to see if the file was opened correctly. This seems
- |> rather inelegant -- I guess exceptions would be the elegant way but seem
- |> like overkill for such a simple program. I've looked in several C++ books but
- |> error handling is given scant coverage except for exception handling.
- |>
- |> Any suggestions would be greatly appreciated.
-
-
- 1. Set a flag in the object, as you suggest.
-
- 2. Throw an exception.
-
- 3. Have each member function test the FILE *, and if it is NULL, return
- an error code. The object "degrades" into a safe, but do-nothing, object.
-
- 4. Print an error message and exit.
-
- If 4. was good enough for your simple C program, why is it not good
- enough for your simple C++ program?
-
- Ian
-